﻿using BP.AIC.Framwork.Repository;
using DotNetCore.CAP;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Order.API.Models;
using Web.MVC.Helper;

namespace Order.Api.Controllers
{
    [Authorize]
    [Route("[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly ICapPublisher _capBus;
        private readonly IOrderRepository<Orders> _orderRepository;
        private readonly IConfiguration _configuration;

     //   private readonly IServiceHelper _serviceHelper;
        private readonly ILogger<OrderController> _logger;
        public OrderController(ILogger<OrderController> logger
           // , IServiceHelper serviceHelper
            , IConfiguration configuration
            , IOrderRepository<Orders> orderRepository
            , ICapPublisher capBus
            ) 
        {
            _logger= logger;
         //   _serviceHelper= serviceHelper;
            _configuration= configuration;
            _orderRepository= orderRepository;
            _capBus= capBus;
        }

        [HttpGet]
        public IActionResult Get() 
        {
            //   _serviceHelper.GetServices();
            Console.WriteLine("order值为" + _configuration["a"]);
            string result = $"--自定义参数AgileConfig：{_configuration["a"]}   --[订单服务]{DateTime.Now.ToString()}--+{Request.HttpContext.Connection.LocalIpAddress}" +
                $":{Request.HttpContext.Connection.LocalPort}--defaultProt:{ _configuration["ConsulSetting:ServicePort"]}";
            return Ok(result);
        }

        [HttpGet("error")]
        public IActionResult GetError()
        {
            //   _serviceHelper.GetServices();
            int i = 1, j = 0;
            int res = i / j;
            return Ok(res);
        }

        [HttpGet("GetProductList")]
        public IEnumerable<string> GetProductList()
        {
            return new List<string>() { "苹果" + Random.Shared.Next(0, 100), "桃子" + Random.Shared.Next(0, 100), "香蕉" + Random.Shared.Next(0, 100) };
        }

        [HttpGet("CrateOrder")]
        public async Task<IActionResult> CrateOrder() 
        {
            Orders orders = new();
            orders.ProductID = DateTime.Now.Millisecond;
            orders.ID= DateTime.Now.Millisecond;
            orders.Count=new Random().Next(1,50);
            orders.CreateTime = DateTime.Now.ToString();
             await _orderRepository.Insertable(orders).ExecuteCommandAsync();

            //制作了Console显示
            _capBus.Publish("order.services.createorder",orders);
            return Ok("当前时间:"+orders.CreateTime);
        }

    }
}
